/*
Kiwi Editor is a framework for building WYSIWYG web editors.
Copyright (C) 2011  Roosevelt de Lima Sardinha

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Contact the author at roosevelt1@gmail.com (Roosevelt) 
*/

/**
 *	A profile is a set of tools grouped by similar objectives. For example, a profile of tools for building forms 
 *	or for building content in HTML.
 *	@author Roosevelt de Lima Sardinha
 *	@constructor	
 */
function profile(){
	/**
	 * The unique id of the profile.
	 * @type {Int}
	 */
	this.id = null;
	
	/**
	 * The name of the profile. A way of making easy its identification by someone.
	 * @type {String}
	 */
	this.name = null;
	
	/**
	 * The description of the profile. Explains what are this set of tools for.
	 * @type {String}
	 */
	this.description = null;
	
	/**
	 * An array of strings containing the ids of all tools in the profile. The ids are the same name of the folder where the tool files are.
	 * @types {Array}
	 */
	this.types = [];
	
	/**
	 * Sets a value to some property of the profile.
	 * @param {String} property The property to be set.
	 * @param {String} value The value to set in the property.
	 */
	this.setProperties = function(property, value){
		switch(property){
			case 'name':
				this.name = value;
				break;
			case 'description':
				this.description = value;
				break;
			case 'types':
				this.types = value;
				break;
			default:
				break;
		}
	}
	
	/**
	 * Adds more tools to the profile.
	 * @param {Array} types_array An array containing all the ids of tools that are going to be added.
	 */
	this.addTool = function(types_array){
		var req  = new request();
		var params = [];
		var json = {};
		json.types = types_array;
		params['json'] = JSON.stringify(json);
		req.sendSincRequest('POST', params, "profiles/" + this.id + "/types" , function(response){});
	}
	
	/**
	 * Removes a tool.
	 * @param {String} type The id of the tool you want to remove.
	 */
	this.removeTool = function(type){
		var req  = new request();		
		var params = [];
		var eoTypeId;
		var allEoTypesStr = "";
		var allEoTypes;
		req.sendSincRequest('GET', params, "eo_types", function(response){
			allEoTypesStr = response;
		});
		allEoTypes = JSON.parse(allEoTypesStr);
		
		for (var i = 0; i < allEoTypes['eo_types'].length; i++){
			if (allEoTypes['eo_types'][i]['name'] == type){
				eoTypeId = allEoTypes['eo_types'][i]['id'];
				break;
			} 
		}
		
		params['_method'] = 'DELETE';
		req.sendSincRequest('POST', params, "profiles/" + this.id + "/types/" + eoTypeId, function(response){});
	}
	
	/**
	 * Saves a profile in the database, for being loaded in another time.
	 */
	this.save = function(){
		var exists = null;
		var id;
		var profileJSON = {};
		profileJSON.name = this.name;
		profileJSON.description = this.description;
		profileJSON.types = this.types;
		var strJSON = JSON.stringify(profileJSON)
		
		var params = [];
		params['json'] = strJSON;
		
		var myRequest = new request();	
		if (this.id != null) {
			myRequest.sendSincRequest('GET', null, "profiles/" + this.id + "/exist", function(response){
				exists = response;
			});
		}
		
		// Check if profile already exists
		if ((exists != null) && JSON.parse(exists)){
			params['_method'] = 'PUT';
	        myRequest.sendSincRequest('POST', params, "profiles/" + this.id, function(response){
			});
		}else{
	        myRequest.sendSincRequest('POST', params, "profiles", function(response){
				id = response;
			});
			this.id = JSON.parse(id);		
		}	
	}
}


